﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Security.Cryptography;
using System.IO;
using SvgApplication.Common;
using SvgApplication.Data.Model;
using System.Linq.Expressions;
using SvgApplication.Data.Model.Interfaces;


namespace SvgApplication.Data.Implementation
{
    public class UserRepository : IUserRepository
    {
        public User Login(string username, string password)
        {
            var hash = HashUtils.GetHash(password);

            var context = new SvgApplicationModelContainer();
            var user = context.Users.FirstOrDefault(p => p.Username == username && p.Password == hash);
            return user;
        }
        public IQueryable<User> GetAll()
        {
            return new SvgApplicationModelContainer().Users.AsQueryable();
        }

        public IQueryable<User> Get(out int total, int index = 0, int size = 50, Expression<Func<User, bool>> filter = null, string orderBy = null)
        {
            var context = new SvgApplicationModelContainer();
            var itemList = context.Users.AsQueryable();
            int skipCount = (index - 1) * size;

            var resetSet = filter != null ? itemList.Where(filter).AsQueryable() : itemList.AsQueryable();

            resetSet = orderBy != null ? resetSet.OrderBy(orderBy).AsQueryable() : resetSet.AsQueryable();

            total = resetSet.Count();

            resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size);

            return resetSet.AsQueryable();
        }



        public void Create(User user)
        {
            var context = new SvgApplicationModelContainer();
            var hash = HashUtils.GetHash(user.Password);
            user.Password = hash;
            context.Users.Add(user);

            context.SaveChanges();
        }


        public User GetById(int id)
        {
            var context = new SvgApplicationModelContainer();
            return context.Users.FirstOrDefault(p => p.Id == id);
        }


        public void Update(User user)
        {
            var context = new SvgApplicationModelContainer();
            var existing = context.Users.First(p => p.Id == user.Id);
            var hash = HashUtils.GetHash(user.Password);
            existing.Password = hash;
            existing.Username = user.Username;
            context.SaveChanges();
        }


        public void Delete(int id)
        {
            var context = new SvgApplicationModelContainer();
            var existing = context.Users.FirstOrDefault(p => p.Id == id);
            if (existing != null)
            {
                context.Users.Remove(existing);
                context.SaveChanges();
            }
        }



        public void UpdateBlockedState(int id, bool blockedState, string reason)
        {
            var context = new SvgApplicationModelContainer();
            var existing = context.Users.FirstOrDefault(p => p.Id == id);
            existing.Blocked = blockedState;
            if (blockedState)
            {
                existing.BlockReason = reason;
            }
            else
            {
                existing.BlockReason = "";
            }
            context.SaveChanges();
        }
    }
}
